home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -readerstuff- / steve_glover / source-code / cx.c < prev    next >
C/C++ Source or Header  |  1999-04-21  |  11KB  |  343 lines

  1.  
  2.    /***********************************************************************
  3.    *                                                                      *
  4.    *                            COPYRIGHTS                                *
  5.    *                                                                      *
  6.    *   Copyright (c) 1990  Commodore-Amiga, Inc.  All Rights Reserved.    *
  7.    *  This file was modified by © Zinneberg-Soft.                         *
  8.    ***********************************************************************/
  9.  
  10. /*
  11.  * cx.c -- Commodities interface
  12.  *
  13.  * This module handles all the command messages from commodities.library.
  14.  * Commands such as HIDE SHOW ENABLE DISABLE KILL are sent to commidities
  15.  * from the Exchange program and are processed here.
  16.  */
  17. #include "local.h"
  18.  
  19. CxObj *broker = NULL;            /* Our broker */
  20.  
  21. /* a little global for showing the user the hotkey   */
  22. char   hotkeybuff[ 257 ];
  23.  
  24. struct NewBroker mynb = {
  25.    NB_VERSION,                        /* Library needs to know version */
  26.    COM_NAME,                          /* broker internal name          */
  27.    COM_TITLE,                         /* commodity title               */
  28.    COM_DESCR,                         /* description                   */
  29.    NBU_NOTIFY | NBU_UNIQUE,           /* We want to be the only broker */
  30.                                       /* with this name and we want to */
  31.                                       /* be notified of any attempts   */
  32.                                       /* to add a commodity with the   */
  33.                                       /* same name                     */
  34.    0,                                 /* flags                         */
  35.    0,                                 /* default priority              */
  36.    NULL,                              /* port, will fill in            */
  37.    0                                  /* channel (reserved)            */
  38. };
  39.  
  40. /****i* Blank.ld/handleCxMsg() ******************************************
  41. *
  42. *   NAME
  43. *        handleCxMsg -- Handle incoming commodities messages.
  44. *
  45. *   SYNOPSIS
  46. *        handleCxMsg(msg)
  47. *
  48. *        VOID handleCxMsg(Struct Message *msg);
  49. *
  50. *   FUNCTION
  51. *        This function handles commodities messages sent to the brokers
  52. *        message port. This is were the standard commodities features
  53. *        such as Enable/Disable Show/Hide Quit and HotKey PopUp are
  54. *        handled. If the  message is not for the standard handler then the
  55. *        function handleCustomCXMsg() is called to handle application
  56. *        specific IEVENTS otherwise handleCustomCXCommand() is called
  57. *        to handle application specific commodities command messages.
  58. *
  59. *   INPUTS
  60. *        msg = A commodities message;
  61. *
  62. *   RESULT
  63. *        Either the standard commodities function is performed or the
  64. *        custom handler is called.
  65. *
  66. *   EXAMPLE
  67. *
  68. *   NOTES
  69. *
  70. *   BUGS
  71. *
  72. *   SEE ALSO
  73. *
  74. *****************************************************************************
  75. *
  76. */
  77.  
  78. VOID handleCxMsg(struct Message *msg)
  79. {
  80.    ULONG   msgid;
  81.    ULONG   msgtype;
  82.  
  83.    msgid   = CxMsgID( (CxMsg *)msg );
  84.    msgtype = CxMsgType( (CxMsg *)msg );
  85.  
  86.    D1( printf("cx.c: handleCxMsg() enter\n") );
  87.  
  88.    ReplyMsg(msg);
  89.  
  90.    switch(msgtype)
  91.    {
  92.       case CXM_IEVENT:
  93.          D1( printf("cx.c: handleCxMsg(CXM_IEVENT)\n") );
  94.          switch(msgid)
  95.          {
  96.             W(
  97.             case POP_KEY_ID:
  98.                   D1( printf("cx.c: handleCxMsg(POP_KEY_ID)\n") );
  99.                   setupWindow();
  100.                   break;
  101.             )
  102.             default:
  103.                   D1( printf("cx.c: handleCxMsg(Custom Message)\n") );
  104.                   handleCustomCXMsg(msgid);
  105.                   break;
  106.          }
  107.          break;
  108.       case CXM_COMMAND:
  109.          D1( printf("cx.c: handleCxMsg(CXM_COMMAND)\n") );
  110.          switch(msgid)
  111.          {
  112.             case CXCMD_DISABLE:
  113.                D1( printf("cx.c: handleCxMsg(CXCMD_DISABLE)\n") );
  114.                ActivateCxObj(broker,0L);
  115.                break;
  116.             case CXCMD_ENABLE:
  117.                D1( printf("cx.c: handleCxMsg(CXCMD_ENABLE)\n") );
  118.                ActivateCxObj(broker,1L);
  119.                break;
  120.             case CXCMD_APPEAR:   /* Time to pop up the window         */
  121.             case CXCMD_UNIQUE:   /* Someone has tried to run us again */
  122.                D1( printf("cx.c: handleCxMsg(CXCMD_APPEAR|CXCMD_UNIQUE)\n") );
  123.                /* If someone tries to run us a second time the second copy
  124.                 * of the program will fail and we will be sent a
  125.                 * CXCMD_UNIQUE message. If we support a window then we
  126.                 * Make our window appear since that is what the user wanted.
  127.                 * If we do not support a window then we kill the currently
  128.                 * running version 'this one' so that things like autopoint
  129.                 * can be toggled on/off by running them a second time.
  130.                 */
  131.                if(WINDOW)
  132.                {
  133.                   W( setupWindow(); )
  134.                } else {
  135.                   terminate();
  136.                }
  137.                break;            /* the window  */
  138.             case CXCMD_DISAPPEAR:
  139.                D1( printf("cx.c: handleCxMsg(CXCMD_DISAPPEAR)\n") );
  140.                W(
  141.                   shutdownWindow();
  142.                )
  143.                break;
  144.             case CXCMD_KILL:
  145.                D1( printf("cx.c: handleCxMsg(CXCMD_KILL)\n") );
  146.                terminate();
  147.                break;
  148.             default:
  149.                D1( printf("cx.c: handleCxMsg(Custom Command)\n") );
  150.                handleCustomCXCommand(msgid);
  151.                break;
  152.          }     /* end switch(command) */
  153.          break;
  154.    }     /* end switch(msgtype) */
  155. }
  156. /****i* Blank.ld/setupCX() ******************************************
  157. *
  158. *   NAME
  159. *        setupCX -- Initialize commodities.library specific features.
  160. *
  161. *   SYNOPSIS
  162. *        result = setupCX(ttypes)
  163. *
  164. *        BOOL setupCX(char **ttypes);
  165. *
  166. *   FUNCTION
  167. *        This function performs all the commodities.library specific
  168. *        setup required for a standard commodity. It sets up the brokers
  169. *        message port, and priority. And sets certain flags in the
  170. *        broker structure so the exchange program will know what
  171. *        features this broker supports. If the commodity supports a window
  172. *        the windows hotkey is added to the broker and then the function
  173. *        setupCustomCX() is called to setup the application specific
  174. *        commodities objects. If all this goes well the broker is activated
  175. *        and the function returns TRUE.
  176. *
  177. *   INPUTS
  178. *        ttypes - NULL terminated Argument array containing this
  179. *        applications TOOLTYPES strings.
  180. *
  181. *   RESULT
  182. *        Returns TRUE if all went OK else returns FALSE.
  183. *
  184. *   EXAMPLE
  185. *        if(setupCX(ttypes))
  186. *        {
  187. *           printf("Commodities successfully initialized.\n");
  188. *        } else {
  189. *           printf("Commodities initialization error!\n");
  190. *        }
  191. *
  192. *   NOTES
  193. *        This function can be called at anytime to reinitialize the
  194. *        commodities code from a new set of arguments.
  195. *
  196. *   BUGS
  197. *
  198. *   SEE ALSO
  199. *        setupCustomCX();
  200. *        shutdownCX();
  201. *        shutodwnCustomCX();
  202. *
  203. *****************************************************************************
  204. *
  205. */
  206. BOOL setupCX(char **ttypes )
  207. {
  208.    LONG   error;
  209.    W( char   *str; )
  210.  
  211.    D1( printf("cx.c: setupCX() enter\n"); )
  212.  
  213.    shutdownCX();                          /* remove what was and create */
  214.                                           /* everything from scratch    */
  215.  
  216.    cxport=CreatePort(mynb.nb_Name,0L);    /* Create Message port        */
  217.    if( ! cxport )
  218.    {
  219.       D1( printf("cx.c: setupCX() Could not create message port\n"); )
  220.       return(FALSE);
  221.    }
  222.    D1( printf("cx.c: setupCX() cxport=0x%lx\n",cxport); )
  223.  
  224.    cxsigflag = 1L << cxport->mp_SigBit;   /* Create signal mask for Wait*/
  225.  
  226.    /* Set the brokers priority from the TOOLTYPES or from default if no */
  227.    /* TOOLTYPES are available. Set the brokers Message port.            */
  228.    mynb.nb_Pri  = ArgInt( ttypes, PRIORITY_TOOL_TYPE, CX_DEFAULT_PRIORITY );
  229.    mynb.nb_Port = cxport;
  230.  
  231.    D1( printf("cx.c: setupCX() mynb.nb_pri=0x%lx\n",mynb.nb_Pri); )
  232.  
  233.    /* If this commodity supports a window then set the SHOW/HIDE flag   */
  234.    /* so the Exchange controller can ghost its gadgets appropriately    */
  235.    W( mynb.nb_Flags |= COF_SHOW_HIDE; )
  236.  
  237.    /* Attempt to create our broker */
  238.    if ( ! ( broker = CxBroker( &mynb, NULL ) ) )
  239.    {
  240.       D1( printf("cx.c: setupCX() could not create broker\n"); )
  241.       shutdownCX();
  242.       return(FALSE);
  243.    }
  244.    D1( printf("cx.c: setupCX() broker=0x%lx\n",broker); )
  245.  
  246.    /* If this commodity supports a window then add its HotKey now  */
  247.    W(
  248.       /* install a hotkey for popping up window   */
  249.       AttachCxObj(broker,
  250.                HotKey(str=ArgString(ttypes,POPKEY_TOOL_TYPE,CX_DEFAULT_POP_KEY),
  251.                cxport,POP_KEY_ID) );
  252.       strncpy(hotkeybuff,str,sizeof(hotkeybuff)-1);
  253.    )
  254.  
  255.    /* Setup all application specific commodities objects */
  256.    if( ! setupCustomCX() )
  257.    {
  258.       D1( printf("cx.c: setupCX() setupCustomCX failed\n"); )
  259.       shutdownCX();
  260.       return(FALSE);
  261.    }
  262.  
  263.    /* Check for accumulated error */
  264.    if ( error = CxObjError( broker ) )
  265.    {
  266.       D1( printf("cx.c: setupCX() accumulated broker error %ld\n",error) );
  267.       shutdownCX();
  268.       return (FALSE);
  269.    }
  270.  
  271.    /* All went well so activate our broker */
  272.    ActivateCxObj(broker,1L);
  273.  
  274.    D1( printf("cx.c: setupCX() returns TRUE") );
  275.    return (TRUE);
  276. }
  277.  
  278. /****i* Blank.ld/shutdownCX() ******************************************
  279. *
  280. *   NAME
  281. *        shutdownCX -- Cleanup all commodities brokers and handlers.
  282. *
  283. *   SYNOPSIS
  284. *        shutdownCX()
  285. *
  286. *        VOID shutdownCX(VOID);
  287. *
  288. *   FUNCTION
  289. *        Shuts down and cleans up all variables and data used for supporting
  290. *        the commodities.library side of this commodity. This function
  291. *        MUST be set up so that it can be called regardless of the current
  292. *        state of the program. This function handles all the standard
  293. *        cleanup and calls shutdownCustomCX(); to cleanup the application
  294. *        specific code.
  295. *
  296. *   INPUTS
  297. *        None.
  298. *
  299. *   RESULT
  300. *        The commodities specific code is cleaned up and made ready for
  301. *        a terminate(); or a call to setupCX();.
  302. *
  303. *   EXAMPLE
  304. *
  305. *   NOTES
  306. *        The first thing that setupCX() does is call this routine. Therefore
  307. *        this function must work even before your commodity has been
  308. *        initialized.
  309. *
  310. *   BUGS
  311. *
  312. *   SEE ALSO
  313. *        setupCX();
  314. *        setupCustomCX();
  315. *        shutdownCustomCX();
  316. *
  317. *****************************************************************************
  318. *
  319. */
  320. VOID shutdownCX()
  321. {
  322.    struct Message   *msg;
  323.  
  324.    D1( printf("cx.c: shutdownCX() enter broker now: %lx\n", broker ) );
  325.    shutdownCustomCX();
  326.  
  327.    if(cxport)
  328.    {
  329.       D1( printf("cx.c: shutdownCX() Deleting all objects\n") );
  330.       DeleteCxObjAll(broker);      /* safe, even if NULL   */
  331.  
  332.       /* now that messages are shut off, clear port   */
  333.       while(msg=GetMsg(cxport)) ReplyMsg(msg);
  334.       DeletePort(cxport);
  335.  
  336.       cxport    = NULL;
  337.       cxsigflag = 0;
  338.       broker    = NULL;
  339.    }
  340.    D1( printf("cx.c: shutdownCX() returns\n") );
  341. }
  342.  
  343.